package com.sunwayworld.basemodule.business.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.File;

public class PDFUtils {
    String filePath = "";

    public PDFUtils(String filePath){
        this.filePath = filePath;
    }

    //读取PDF中的数据
    public String parsePDF(){
        String result = "";
        try {
            PDDocument load = PDDocument.load(new File(filePath));
            PDFTextStripper stripper = new PDFTextStripper();
            stripper.setSortByPosition(true);
            for (int i = 1; i < load.getNumberOfPages()+1; i++) {
                //读取pdf的开始到结束页
                stripper.setStartPage(i);
                stripper.setEndPage(i);
                String text = stripper.getText(load);
                //拼接不同页数的数据返回
                result += text;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
